package boundary;

import java.awt.GridBagLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.Icon;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import store.BaseStore;
import store.Ricetta;
import store.Termine;
import store.Terminologia;
import dataview.Difficolta;
import dataview.InfoRicetta;
import dataview.Termini;
import dataview.TipoTermine;
import dataview.UnitaPorzioni;
import exceptions.DBException;
import javax.swing.JButton;
import java.awt.Cursor;
import java.awt.Insets;
import java.awt.Font;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import java.awt.Dimension;
import javax.swing.JScrollPane;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;

public class ModificaInfoRicette extends JPanel implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	private Home home;
	private Ricetta ricetta;
	private Set<String> tagTipoPreparazione; //  @jve:decl-index=0:
	private Set<String> tagGenCucina; //  @jve:decl-index=0:
	private Set<String> tagGenGeog;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;
	private JTextField jTextField = null;
	private JLabel jLabel4 = null;
	private JComboBox unitaCB = null;
	private JLabel jLabel5 = null;
	private JButton avantiB = null;
	private JComboBox cbTipoPreparazione = null;
	private JComboBox cbGenCucina = null;
	private JLabel lGenCucina = null;
	private JLabel lTipoPreparazione = null;
	private JScrollPane spPres = null;
	private JTextArea presentazioneTA = null;
	private JScrollPane spNote = null;
	private JTextArea noteTA = null;
	private JLabel lGenGeografico = null;
	private JComboBox cbGenGeog = null;
	private JPanel starsPanel = null;
	
	private Star stars[] = new Star[3];
	private JTextField porzioneCustom = null;
	private JButton modificheInfo = null;
	
	/**
	 * This is the default constructor
	 */
	public ModificaInfoRicette(Home home, Ricetta ricetta) {
		super();
		this.home = home;
		this.ricetta = ricetta;
		initialize();
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints2.gridy = 8;
		gridBagConstraints2.weightx = 1.0;
		gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints2.gridx = 2;
		GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
		gridBagConstraints13.gridx = 1;
		gridBagConstraints13.gridwidth = 2;
		gridBagConstraints13.gridy = 9;
		gridBagConstraints13.insets = new Insets(5, 5, 5, 5);
		GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
		gridBagConstraints14.fill = GridBagConstraints.BOTH;
		gridBagConstraints14.gridy = 3;
		gridBagConstraints14.weightx = 1.0;
		gridBagConstraints14.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints14.anchor = GridBagConstraints.WEST;
		gridBagConstraints14.gridwidth = 2;
		gridBagConstraints14.gridx = 1;
		GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
		gridBagConstraints12.gridx = 0;
		gridBagConstraints12.anchor = GridBagConstraints.WEST;
		gridBagConstraints12.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints12.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints12.gridy = 3;
		lGenGeografico = new JLabel();
		lGenGeografico.setText("Genere geografico");
		lGenGeografico.setFont(new Font("Dialog", Font.BOLD, 12));
		GridBagConstraints gridBagConstraints101 = new GridBagConstraints();
		gridBagConstraints101.fill = GridBagConstraints.BOTH;
		gridBagConstraints101.gridy = 2;
		gridBagConstraints101.weightx = 1.0;
		gridBagConstraints101.anchor = GridBagConstraints.WEST;
		gridBagConstraints101.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints101.gridwidth = 2;
		gridBagConstraints101.gridx = 1;
		GridBagConstraints gridBagConstraints91 = new GridBagConstraints();
		gridBagConstraints91.fill = GridBagConstraints.BOTH;
		gridBagConstraints91.gridy = 1;
		gridBagConstraints91.weightx = 1.0;
		gridBagConstraints91.anchor = GridBagConstraints.WEST;
		gridBagConstraints91.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints91.gridwidth = 2;
		gridBagConstraints91.gridx = 1;
		GridBagConstraints gridBagConstraints81 = new GridBagConstraints();
		gridBagConstraints81.fill = GridBagConstraints.BOTH;
		gridBagConstraints81.gridy = 6;
		gridBagConstraints81.weightx = 1.0;
		gridBagConstraints81.weighty = 1.0;
		gridBagConstraints81.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints81.gridwidth = 2;
		gridBagConstraints81.gridx = 1;
		GridBagConstraints gridBagConstraints71 = new GridBagConstraints();
		gridBagConstraints71.fill = GridBagConstraints.BOTH;
		gridBagConstraints71.gridy = 4;
		gridBagConstraints71.weightx = 1.0;
		gridBagConstraints71.weighty = 1.0;
		gridBagConstraints71.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints71.gridwidth = 2;
		gridBagConstraints71.gridx = 1;
		GridBagConstraints gridBagConstraints61 = new GridBagConstraints();
		gridBagConstraints61.gridx = 0;
		gridBagConstraints61.anchor = GridBagConstraints.WEST;
		gridBagConstraints61.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints61.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints61.gridy = 1;
		lTipoPreparazione = new JLabel();
		lTipoPreparazione.setText("Tipo preparazione");
		lTipoPreparazione.setFont(new Font("Dialog", Font.BOLD, 12));
		GridBagConstraints gridBagConstraints41 = new GridBagConstraints();
		gridBagConstraints41.gridx = 0;
		gridBagConstraints41.anchor = GridBagConstraints.WEST;
		gridBagConstraints41.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints41.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints41.gridy = 2;
		lGenCucina = new JLabel();
		lGenCucina.setText("Genere cucina");
		lGenCucina.setFont(new Font("Dialog", Font.BOLD, 12));
		GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
		gridBagConstraints31.fill = GridBagConstraints.NONE;
		gridBagConstraints31.gridy = 2;
		gridBagConstraints31.weightx = 1.0;
		gridBagConstraints31.anchor = GridBagConstraints.WEST;
		gridBagConstraints31.insets = new Insets(0, 3, 0, 0);
		gridBagConstraints31.gridx = 1;
		GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
		gridBagConstraints11.gridx = 0;
		gridBagConstraints11.fill = GridBagConstraints.NONE;
		gridBagConstraints11.gridwidth = 3;
		gridBagConstraints11.anchor = GridBagConstraints.CENTER;
		gridBagConstraints11.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints11.ipady = 0;
		gridBagConstraints11.ipadx = 0;
		gridBagConstraints11.weightx = 0.0;
		gridBagConstraints11.gridy = 10;
		GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
		gridBagConstraints10.fill = GridBagConstraints.NONE;
		gridBagConstraints10.gridy = 9;
		gridBagConstraints10.weightx = 1.0;
		gridBagConstraints10.anchor = GridBagConstraints.WEST;
		gridBagConstraints10.insets = new Insets(3, 3, 3, 3);
		gridBagConstraints10.gridx = 1;
		GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
		gridBagConstraints9.gridx = 0;
		gridBagConstraints9.anchor = GridBagConstraints.WEST;
		gridBagConstraints9.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints9.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints9.gridy = 9;
		jLabel5 = new JLabel();
		jLabel5.setText("Difficolta'");
		jLabel5.setFont(new Font("Dialog", Font.BOLD, 12));
		GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
		gridBagConstraints8.fill = GridBagConstraints.BOTH;
		gridBagConstraints8.gridy = 8;
		gridBagConstraints8.weightx = 1.0;
		gridBagConstraints8.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints8.anchor = GridBagConstraints.WEST;
		gridBagConstraints8.gridx = 1;
		GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
		gridBagConstraints7.gridx = 0;
		gridBagConstraints7.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints7.anchor = GridBagConstraints.WEST;
		gridBagConstraints7.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints7.gridy = 8;
		jLabel4 = new JLabel();
		jLabel4.setText("Unita porzioni *");
		jLabel4.setFont(new Font("Dialog", Font.BOLD, 12));
		GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
		gridBagConstraints6.fill = GridBagConstraints.BOTH;
		gridBagConstraints6.gridy = 7;
		gridBagConstraints6.weightx = 1.0;
		gridBagConstraints6.anchor = GridBagConstraints.WEST;
		gridBagConstraints6.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints6.gridwidth = 2;
		gridBagConstraints6.gridx = 1;
		GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
		gridBagConstraints5.gridx = 0;
		gridBagConstraints5.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints5.anchor = GridBagConstraints.WEST;
		gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints5.gridy = 7;
		jLabel3 = new JLabel();
		jLabel3.setText("Numero porzioni *");
		jLabel3.setFont(new Font("Dialog", Font.BOLD, 12));
		GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
		gridBagConstraints3.gridx = 0;
		gridBagConstraints3.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints3.anchor = GridBagConstraints.NORTHWEST;
		gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints3.gridy = 6;
		jLabel2 = new JLabel();
		jLabel2.setText("Note");
		jLabel2.setFont(new Font("Dialog", Font.BOLD, 12));
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.gridx = 0;
		gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints1.anchor = GridBagConstraints.NORTHWEST;
		gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints1.gridy = 4;
		jLabel1 = new JLabel();
		jLabel1.setText("Presentazione *");
		jLabel1.setFont(new Font("Dialog", Font.BOLD, 12));
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridwidth = 3;
		gridBagConstraints.gridy = 0;
		jLabel = new JLabel();
		jLabel.setText(ricetta.getNome());
		jLabel.setFont(new Font("Dialog", Font.BOLD, 22));
		this.setSize(356, 343);
		this.setLayout(new GridBagLayout());
		this.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		this.add(jLabel, gridBagConstraints);
		this.add(jLabel1, gridBagConstraints1);
		this.add(jLabel2, gridBagConstraints3);
		this.add(jLabel3, gridBagConstraints5);
		this.add(getJTextField(), gridBagConstraints6);
		this.add(jLabel4, gridBagConstraints7);
		this.add(getUnitaCB(), gridBagConstraints8);
		this.add(jLabel5, gridBagConstraints9);
		this.add(getAvantiB(), gridBagConstraints11);
		this.add(lGenCucina, gridBagConstraints41);
		this.add(lTipoPreparazione, gridBagConstraints61);
		this.add(getSpPres(), gridBagConstraints71);
		this.add(getSpNote(), gridBagConstraints81);
		this.add(getCbTipoPreparazione(), gridBagConstraints91);
		this.add(getCbGenCucina(), gridBagConstraints101);
		this.add(lGenGeografico, gridBagConstraints12);
		this.add(getCbGenGeog(), gridBagConstraints14);
		this.add(getStarsPanel(), gridBagConstraints13);
		this.add(getPorzioneCustom(), gridBagConstraints2);
		
		if (ricetta.getInfo() != null) {
			if (ricetta.getInfo().getNote() != null) getNoteTA().setText(ricetta.getInfo().getNote());
			getPresentazioneTA().setText(ricetta.getInfo().getPresentazione());
			if (ricetta.getInfo().getDifficolta() != null) stars[ricetta.getInfo().getDifficolta().ordinal()].setAttivato(true);
			if (!ricetta.getTag().getTerminiDiTipo(TipoTermine.genere_geografico).isEmpty())
				getCbGenGeog().setSelectedItem(ricetta.getTag().getTerminiDiTipo(TipoTermine.genere_geografico).getFirst().toString());
			if (!ricetta.getTag().getTerminiDiTipo(TipoTermine.tipo_preparazione).isEmpty())
				getCbTipoPreparazione().setSelectedItem(ricetta.getTag().getTerminiDiTipo(TipoTermine.tipo_preparazione).getFirst().toString());
			if (!ricetta.getTag().getTerminiDiTipo(TipoTermine.genere_cucina).isEmpty())
				getCbGenCucina().setSelectedItem(ricetta.getTag().getTerminiDiTipo(TipoTermine.genere_cucina).getFirst().toString());
			unitaCB.setSelectedItem(ricetta.getInfo().getUnitaPorzioni().toString());
			jTextField.setText(ricetta.getInfo().getNumeroPorzioni() + "");
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.gridx = 0;
			gbc.fill = GridBagConstraints.NONE;
			gbc.gridwidth = 3;
			gbc.anchor = GridBagConstraints.CENTER;
			gbc.insets = new Insets(5, 5, 5, 5);
			gbc.gridy = 11;
			this.add(getSalvaModificheInfo(), gbc);
		}
	}
	
	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setColumns(20);
			jTextField.setPreferredSize(new Dimension(100, 19));
			jTextField.setFont(new Font("Dialog", Font.PLAIN, 12));
			jTextField.addKeyListener(new NumericAdapter());
		}
		return jTextField;
	}
	
	/**
	 * This method initializes unitaCB
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getUnitaCB() {
		if (unitaCB == null) {
			Set<String> unitaSet = new TreeSet<String>();
			for (UnitaPorzioni u : UnitaPorzioni.values())
				unitaSet.add(u.toString());
			unitaSet.add("Seleziona unita'..");
			unitaCB = new JComboBox(unitaSet.toArray());
			unitaCB.setFont(new Font("Dialog", Font.PLAIN, 12));
			unitaCB.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getPorzioneCustom().setVisible(getUnitaCB().getSelectedItem() == "altro");
					home.setVisible(true);
				}
			});
		}
		return unitaCB;
	}
	
	/**
	 * This method initializes avantiB
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAvantiB() {
		if (avantiB == null) {
			avantiB = new JButton();
			if (ricetta.getInfo() == null)
				avantiB.setText("Inserisci procedimento");
			else avantiB.setText("Modifica procedimento");
			avantiB.setMnemonic(KeyEvent.VK_UNDEFINED);
			avantiB.setIcon(new ImageIcon(getClass().getResource("/icone/accept-icon.png")));
			avantiB.setFont(new Font("Dialog", Font.BOLD, 12));
			avantiB.addActionListener(this);
		}
		return avantiB;
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		do {
			String pres = getPresentazioneTA().getText();
			if (pres == null || pres.length() < 1) {
				errorMessage("Inserire una presentazione per la ricetta");
				break;
			} else if (pres.length() < 200) {
				errorMessage("Inserire una presentazione piu` lunga");
				break;
			}
			String note = getNoteTA().getText();
			String sPorz = getJTextField().getText();
			if (sPorz == null || sPorz.length() < 1 || sPorz.length() > 5) {
				errorMessage("Inserire un numero di porzioni valido");
				break;
			}
			if (getUnitaCB().getSelectedIndex() == 0) {
				errorMessage("Selezionare una unita' di misura per le porzioni");
				break;
			}
			UnitaPorzioni u = UnitaPorzioni.valueOf((String)getUnitaCB().getSelectedItem());
			Termini tag = new Termini();
			if (cbTipoPreparazione.getSelectedIndex() != 0) tag.add(new Termine(TipoTermine.tipo_preparazione, (String)cbTipoPreparazione.getSelectedItem()));
			if (cbGenCucina.getSelectedIndex() != 0) tag.add(new Termine(TipoTermine.genere_cucina, (String)cbGenCucina.getSelectedItem()));
			if (cbGenGeog.getSelectedIndex() != 0) tag.add(new Termine(TipoTermine.genere_geografico, (String)cbGenGeog.getSelectedItem()));
			
			if (u == UnitaPorzioni.altro)
				ricetta.modifica(new InfoRicetta(pres, note, Integer.valueOf(sPorz), getDifficolta(), getPorzioneCustom().getText()), tag);
			else ricetta.modifica(new InfoRicetta(pres, note, Integer.valueOf(sPorz), u, getDifficolta()), tag);
			if (arg0.getSource() == avantiB) {
				home.visualizzaProcedimento(ricetta);
			} else {
				try {
					if (BaseStore.salvaModifiche(ricetta))
						JOptionPane.showMessageDialog(this, "Ricetta modificata correttamente", "Ricetta modificato", JOptionPane.INFORMATION_MESSAGE);
				} catch (Exception exc) {
					exc.printStackTrace();
					JOptionPane.showMessageDialog(this, "Ricetta non modificata a causa di un errore", "Errore", JOptionPane.ERROR_MESSAGE);
				}
				home.mostraListaRicette(null);
			}
		} while (false);
	}
	
	private void errorMessage(String message) {
		JOptionPane.showMessageDialog(this, message, "Errore", JOptionPane.ERROR_MESSAGE);
	}
	
	/**
	 * This method initializes cbTipoPreparazione
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getCbTipoPreparazione() {
		if (cbTipoPreparazione == null) {
			try {
				tagTipoPreparazione = Terminologia.getTerminologia().getTuttiTermini(TipoTermine.tipo_preparazione);
				tagTipoPreparazione.add("Seleziona tipo preparazione..");
				cbTipoPreparazione = new JComboBox(tagTipoPreparazione.toArray());
				cbTipoPreparazione.setFont(new Font("Dialog", Font.PLAIN, 12));
			} catch (DBException e) {
				e.printStackTrace();
			}
		}
		return cbTipoPreparazione;
	}
	
	/**
	 * This method initializes cbOccasione
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getCbGenCucina() {
		if (cbGenCucina == null) {
			try {
				tagGenCucina = Terminologia.getTerminologia().getTuttiTermini(TipoTermine.genere_cucina);
				tagGenCucina.add("Seleziona genere cucina..");
				cbGenCucina = new JComboBox(tagGenCucina.toArray());
				cbGenCucina.setFont(new Font("Dialog", Font.PLAIN, 12));
			} catch (DBException e) {
				e.printStackTrace();
			}
		}
		return cbGenCucina;
	}
	
	/**
	 * This method initializes spPres
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getSpPres() {
		if (spPres == null) {
			spPres = new JScrollPane();
			spPres.setViewportView(getPresentazioneTA());
		}
		return spPres;
	}
	
	/**
	 * This method initializes presentazioneTA
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getPresentazioneTA() {
		if (presentazioneTA == null) {
			presentazioneTA = new JTextArea();
			presentazioneTA.setFont(new Font("Dialog", Font.PLAIN, 12));
			presentazioneTA.setLineWrap(true);
			presentazioneTA.setWrapStyleWord(true);
			presentazioneTA.setTabSize(0);
			presentazioneTA.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyPressed(java.awt.event.KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_TAB) {
						if (e.isShiftDown())
							getCbGenGeog().requestFocus();
						else getNoteTA().requestFocus();
					}
				}
			});
		}
		return presentazioneTA;
	}
	
	/**
	 * This method initializes spNote
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getSpNote() {
		if (spNote == null) {
			spNote = new JScrollPane();
			spNote.setViewportView(getNoteTA());
		}
		return spNote;
	}
	
	/**
	 * This method initializes noteTA
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getNoteTA() {
		if (noteTA == null) {
			noteTA = new JTextArea();
			noteTA.setFont(new Font("Dialog", Font.PLAIN, 12));
			noteTA.setLineWrap(true);
			noteTA.setWrapStyleWord(true);
			noteTA.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyPressed(java.awt.event.KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_TAB) {
						if (e.isShiftDown())
							getPresentazioneTA().requestFocus();
						else getJTextField().requestFocus();
					}
				}
			});
		}
		return noteTA;
	}
	
	/**
	 * This method initializes cbGenGeog
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getCbGenGeog() {
		if (cbGenGeog == null) {
			try {
				tagGenGeog = Terminologia.getTerminologia().getTuttiTermini(TipoTermine.genere_geografico);
				tagGenGeog.add("Seleziona genere geografico..");
				cbGenGeog = new JComboBox(tagGenGeog.toArray());
				cbGenGeog.setFont(new Font("Dialog", Font.PLAIN, 12));
			} catch (DBException e) {
				e.printStackTrace();
			}
		}
		return cbGenGeog;
	}
	
	/**
	 * This method initializes starsPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getStarsPanel() {
		if (starsPanel == null) {
			starsPanel = new JPanel();
			
			starsPanel.setLayout(new GridBagLayout());
			for (int i = 0; i < stars.length; i++) {
				GridBagConstraints gbc = new GridBagConstraints();
				gbc.gridx = i;
				gbc.gridy = 0;
				gbc.insets = new Insets(2, 2, 2, 2);
				stars[i] = new Star();
				stars[i].setToolTipText(Difficolta.values()[i].toString());
				starsPanel.add(stars[i], gbc);
			}
		}
		return starsPanel;
	}
	
	private int ultimoAttivato() {
		for (int i = stars.length - 1; i >= 0; i--)
			if (stars[i].isAttivato()) return i;
		return -1;
	}
	
	private Difficolta getDifficolta() {
		int i = ultimoAttivato();
		if (i != -1) return Difficolta.values()[i];
		return null;
	}
	
	private class Star extends JLabel {
		private boolean attivato = false;
		private final Icon ATTIVATO_ICON = new ImageIcon(getClass().getResource("/icone/gold-star.gif"));
		private final Icon DISATTIVATO_ICON = new ImageIcon(getClass().getResource("/icone/silver-star.gif"));
		private final Icon HOVER_ICON = new ImageIcon(getClass().getResource("/icone/platinum-star.gif"));
		private final Icon ATTIVATO_HOVER_ICON = new ImageIcon(getClass().getResource("/icone/bronze-star.gif"));
		
		Star() {
			super();
			setIcon(DISATTIVATO_ICON);
			setCursor(new Cursor(Cursor.HAND_CURSOR));
			addMouseListener(new MouseAdapter() {
				public void mousePressed(MouseEvent e) {
					int i = indexOf();
					if (attivato && i != ultimoAttivato())
						stars[i + 1].setAttivato(!getAttivato());
					else setAttivato(!getAttivato());
				}
				
				public void mouseEntered(MouseEvent e) {
					if (!attivato) {
						for (int i = 0; i <= indexOf(); i++)
							stars[i].setIcon(ATTIVATO_ICON);
					} else setIcon(ATTIVATO_HOVER_ICON);
				}
				
				public void mouseExited(MouseEvent e) {
					if (!attivato)
						for (int i = 0; i <= indexOf(); i++)
							if (stars[i].isAttivato())
								stars[i].setIcon(ATTIVATO_ICON);
							else stars[i].setIcon(DISATTIVATO_ICON);
					else setIcon(ATTIVATO_ICON);
				}
			});
		}
		
		boolean isAttivato() {
			return attivato;
		}
		
		void setAttivato(boolean b) {
			if (attivato != b) {
				attivato = b;
				int i = indexOf();
				if (attivato) {
					setIcon(ATTIVATO_ICON);
					if (indexOf() > 0) stars[i - 1].setAttivato(attivato);
				} else {
					setIcon(DISATTIVATO_ICON);
					if (indexOf() < stars.length - 1) stars[i + 1].setAttivato(attivato);
				}
			}
		}
		
		public boolean getAttivato() {
			return attivato;
		}
		
		private int indexOf() {
			for (int i = 0; i < stars.length; i++)
				if (this == stars[i]) return i;
			return -1;
		}
	}
	
	/**
	 * This method initializes porzioneCustom
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getPorzioneCustom() {
		if (porzioneCustom == null) {
			porzioneCustom = new JTextField();
			porzioneCustom.setVisible(false);
			porzioneCustom.setText(ricetta.getInfo() == null || ricetta.getInfo().getCustom() == null ? "" : ricetta.getInfo().getCustom());
		}
		return porzioneCustom;
	}
	
	/**
	 * This method initializes modificheInfo
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getSalvaModificheInfo() {
		if (modificheInfo == null) {
			modificheInfo = new JButton();
			modificheInfo.setVisible(true);
			modificheInfo.setText("Salva le modifiche alle informazioni");
			modificheInfo.setIcon(new ImageIcon(getClass().getResource("/icone/accept-icon.png")));
			modificheInfo.setFont(new Font("Dialog", Font.BOLD, 12));
			modificheInfo.addActionListener(this);
		}
		return modificheInfo;
	}
	
} //  @jve:decl-index=0:visual-constraint="0,0"
